import {ref} from 'vue'

export const useCountDown = (n = 60) => {
  const count = ref(n)
  const timer = ref(Number.MIN_VALUE)
  const isDone = ref(false)


  const countDown = () => {
    if (isDone.value) return
    isDone.value = true
    timer.value = setInterval(() => {
      if (count.value === 0) {
        count.value = n
        isDone.value = false
        clearInterval(timer.value)
        timer.value = Number.MIN_VALUE
      } else {
        count.value = count.value - 1
      }
    }, 1000)
  }

  return { count, isDone, countDown }
}
